/*
 * Created by Huang 嵘才 on 18-2-7 上午11:22
 * Copyright (c) 2018. All rights reserved.
 * Last modified 18-2-6 下午4:57
 */

package io.esirong.image.gilde;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestBuilder;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.Target;

import androidx.annotation.Nullable;
import io.esirong.image.ImageLoaderOptions;
import io.esirong.image.ImageLoaderStrategy;


/**
 * GlideImageLoader
 * Created by 黄嵘才(HuangRc) on 2018/2/1.
 */

public class GlideImageLoader implements ImageLoaderStrategy {

    private static volatile GlideImageLoader imageLoader;

    private GlideImageLoader() {
    }

    /**
     * Get the singleton.
     *
     * @return the singleton
     */
    public static GlideImageLoader get() {
        if (imageLoader == null) {
            synchronized (GlideImageLoader.class) {
                if (imageLoader == null) {
                    imageLoader = new GlideImageLoader();
                }
            }
        }
        return imageLoader;
    }

    @Override
    public void init(Context context) {

    }

    @Override
    public void showImage(final View v, String url, final ImageLoaderOptions options) {
        if (v instanceof ImageView) {
            ImageView imageView = (ImageView) v;
            RequestBuilder builder = createBuilder(v, url, options);
            builder.into(imageView);
        }
    }

    @Override
    public void showImage(View v, int drawable, final ImageLoaderOptions options) {
        if (v instanceof ImageView) {
            ImageView imageView = (ImageView) v;
            RequestBuilder builder = createBuilder(v, drawable, options);
            builder.into(imageView);
        }
    }

    @Override
    public void showImage(View v, byte[] imageBytes, ImageLoaderOptions options) {
        if (v instanceof ImageView) {
            ImageView imageView = (ImageView) v;
            RequestBuilder builder = createBuilder(v, imageBytes, options);
            builder.into(imageView);
        }
    }

    private RequestBuilder createBuilder(View view, Object drawable,
            final ImageLoaderOptions options) {
        RequestOptions requestOptions = loadOptions(options);
        RequestBuilder builder = Glide.with(view)
                .load(drawable)
                .apply(requestOptions)
                .listener(new RequestListener<Drawable>() {
                    @Override
                    public boolean onLoadFailed(@Nullable GlideException e,
                            Object model,
                            Target<Drawable> target,
                            boolean isFirstResource) {
                        if (options.getLoaderResultCallBack() != null) {
                            options.getLoaderResultCallBack().onFailed();
                        }
                        return false;
                    }

                    @Override
                    public boolean onResourceReady(Drawable resource,
                            Object model,
                            Target<Drawable> target,
                            DataSource dataSource,
                            boolean isFirstResource) {
                        if (options.getLoaderResultCallBack() != null) {
                            options.getLoaderResultCallBack().onSuccess();
                        }

                        return false;
                    }
                });
        return builder;
    }

    /**
     * 这个方法用来装载由外部设置的参数
     */
    private RequestOptions loadOptions(ImageLoaderOptions options) {
        RequestOptions requestOptions = new RequestOptions();

        if (options.getHolderDrawable() != -1) {
            requestOptions.placeholder(options.getHolderDrawable());
        }
        if (options.getErrorDrawable() != -1) {
            requestOptions.fallback(options.getErrorDrawable());
        }

        if (options.getDiskCacheStrategy() != ImageLoaderOptions.DiskCacheStrategy.DEFAULT) {
            switch (options.getDiskCacheStrategy()) {
                case NONE:
                    requestOptions.diskCacheStrategy(DiskCacheStrategy.NONE);
                    break;
                case All:
                    requestOptions.diskCacheStrategy(DiskCacheStrategy.ALL);
                    break;
                case SOURCE:
                    requestOptions.diskCacheStrategy(DiskCacheStrategy.RESOURCE);
                    break;
                case RESULT:
                    requestOptions.diskCacheStrategy(DiskCacheStrategy.DATA);
                    break;
                default:
                    break;
            }
        }
        switch (options.getScaleType()) {
            case CENTER_CROP:
                requestOptions.centerCrop();
                break;
            case CENTER_INSIDE:
                requestOptions.centerInside();
                break;
            default:
                requestOptions.centerInside();
                break;

        }
        if (options.isDontAnimate()) {
            requestOptions.dontAnimate();
        }

        if (options.isSkipMemoryCache()) {
            requestOptions.skipMemoryCache(true);
        }
        if (options.getImageSize() != null) {
            requestOptions.override(options.getImageSize().getWidth(), options.getImageSize().getHeight());
        }

        if (options.isBlurImage()) {

        }
        if (options.needImageRadius()) {
            requestOptions.transform(new RoundedCorners(options.getImageRadius()));
        }
        if (options.isCircle()) {
            requestOptions.circleCrop();
        }


        return requestOptions;
    }
}
